package com.youchuang.util.tag;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.SimpleTagSupport;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.youchuang.action.cart.CartItem;
import com.youchuang.bean.Book;
import com.youchuang.bean.Product;
import com.youchuang.common.Constant;
import com.youchuang.util.TransforDigitUtil;

public class RetainTwoNumTag extends SimpleTagSupport {
	private Logger logger = Logger.getLogger(RetainTwoNumTag.class.getSimpleName());
	private double fixedPrice;
	private double dangPrice;

	public void doTag() throws JspException, IOException {
		logger.info("method is doTag");
		PageContext ctxt = (PageContext) getJspContext();// 获得页面上下文
		JspWriter out = ctxt.getOut();// 定义页面输出流
		HttpServletRequest request = ServletActionContext.getRequest();
		Product product = new Product();
		if (request.getAttribute("b") != null) {
			product = (Product) request.getAttribute("b");
		} else if (request.getAttribute("ci") != null) {
			CartItem ci = (CartItem) request.getAttribute("ci");
			product = ci.getProduct();
		} else if (request.getAttribute(Constant.PRODUCT) != null) {
			product = (Product) request.getAttribute(Constant.PRODUCT);
		} else if (request.getAttribute(Constant.BOOK) != null) {
			Book book = (Book) request.getAttribute(Constant.BOOK);
			product = book.getProduct();
		} else if (request.getAttribute(Constant.ITEM) != null) {
			CartItem ci = (CartItem) request.getAttribute(Constant.ITEM);
			product = ci.getProduct();
			dangPrice = product.getDangPrice();
			double cPrice = TransforDigitUtil.transformNumber(dangPrice * ci.getNum());
			out.println(cPrice);
			return;
		}

		if (product == null)
			return;
		fixedPrice = product.getFixedPrice();
		dangPrice = product.getDangPrice();
		double savePrice = fixedPrice - dangPrice;
		savePrice = TransforDigitUtil.transformNumber(savePrice);
		out.println(savePrice);
	}

}
